package com.jc.base.page;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

/**
 * 应用分页的对象
 * 
 * @author lrh
 * @create 2014年12月19日下午3:14:29
 */
public class Page implements Serializable{

	private static final long serialVersionUID = 1L;

	private static final Integer DEFUALT_PAGE_SIZE = 15;

	private static final Integer DEFAULT_TOTAL_NUM = 0;
	/**
	 * 分页中，页面大小，即每页显示多少的数据
	 */
	private Integer pageSize = DEFUALT_PAGE_SIZE;

	private Integer currentPage = 1;

	private Integer totalNum = DEFAULT_TOTAL_NUM;

	private Integer startIndex;

	private Integer endIndex;
	
	private Integer pageNum;//总页数

	public Page(Integer pageSize, Integer currentPage) {
		this.pageSize = pageSize;
		setCurrentPage(currentPage);
	}

	public Page(Integer pageSize, Integer currentPage, Integer totalNum) {
		super();
		this.pageSize = pageSize;
		setCurrentPage(currentPage);
		setTotalNum(totalNum);
	}

	public Page() {
		setPageSize(DEFUALT_PAGE_SIZE);
	}
	
	@JsonIgnore
	public Integer getTotalPageNum() {
		if (totalNum == 0) {
			return Integer.valueOf(0);
		}
		this.pageNum = totalNum / pageSize + (totalNum % pageSize == 0 ? 0 : 1);
		
		return pageNum;
	}

	/**
	 * @return the pageSize
	 */
	public Integer getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(Integer pageSize) {
		if (pageSize == null) {
			this.pageSize = DEFUALT_PAGE_SIZE;
		} else {
			this.pageSize = pageSize;
		}
		caculatIndex();
	}

	/**
	 * @return the currentPage
	 */
	public Integer getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(Integer currentPage) {
		if (currentPage == null) {
			this.currentPage = Integer.valueOf(1);
		} else {
			this.currentPage = currentPage;
		}
		caculatIndex();
	}

	/**
	 * @return the totalNum
	 */
	public Integer getTotalNum() {
		return totalNum;
	}

	/**
	 * @param totalNum
	 *            the totalNum to set
	 */
	public void setTotalNum(Integer totalNum) {
		if (totalNum == null) {
			this.totalNum = DEFAULT_TOTAL_NUM;
		}else{
			this.totalNum = totalNum;			
		}
		getTotalPageNum();
	}

	private void caculatIndex() {
		if (this.currentPage == null || this.currentPage <= 1) {
			this.currentPage = 1;
		}
		if (this.pageSize == null || this.pageSize <= 0) {
			this.pageSize = DEFUALT_PAGE_SIZE;
		}
		this.startIndex = (this.currentPage - 1) * this.pageSize;
		this.endIndex = this.pageSize;
	}
	
	@JsonIgnore
	public Integer getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}
	
	@JsonIgnore
	public Integer getEndIndex() {
		return endIndex;
	}

	public void setEndIndex(Integer endIndex) {
		this.endIndex = endIndex;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	
}
